package cn.owng.server.common.validation.constraintvalidators;

import cn.owng.server.common.validation.constraints.Option;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
public class OptionValidator implements ConstraintValidator<Option, String> {

    private String[] options;

    @Override
    public void initialize(Option annotation) {
        this.options = annotation.options();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(value)) {
            return true;
        }
        return options != null && Arrays.asList(options).contains(value);
    }
}
